package com.bestcem.xm.member.mq.receiver;//package com.bestcem.xm.member.mq.receiver;
//
//import com.alibaba.fastjson.JSON;
//import com.bestcem.xm.member.mq.MemberTopics;
//import com.bestcem.xm.member.mq.message.GroupingMessage;
//import com.bestcem.xm.member.mq.message.MemberCreationUpdMessage;
//import com.bestcem.xm.member.mq.message.WechatMemberSyncMessage;
//import com.bestcem.xm.member.service.GroupService;
//import com.bestcem.xm.member.service.WechatService;
//import com.bestcem.xm.util.MsgPackFormatUtil;
//import com.bestcem.xm.common.core.domain.web.ServiceResult;
//import com.rabbitmq.client.Channel;
//import lombok.extern.slf4j.Slf4j;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.amqp.core.Message;
//import org.springframework.amqp.rabbit.annotation.RabbitHandler;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.io.IOException;
//import java.util.Base64;
//
///**
// * 监听联系人延迟队列消息
// *
// * @author ympan <yuming.pan@idiaoyan.com>
// * @version v1.0
// * @date 16:55 2022/1/17
// */
//@Slf4j
//@Component
//public class MemberDelayQueueRabbitReceive {
//
//    @Autowired
//    private GroupService groupService;
//
//    @Autowired
//    private WechatService wechatService;
//
//    private static final Logger EMAIL_LOG = LoggerFactory.getLogger("EmailErrorLog");
//
//    /**
//     * 监听联系人延迟队列消息
//     */
//    @RabbitListener(queues = "#{appMemberDelayQueue.name}", concurrency = "${app.member.rabbitmq.memberDelayQueue.concurrency:2}")
//    @RabbitHandler
//    public void handlerApp(Message message, Channel channel) {
//        try {
//            if (log.isInfoEnabled()) {
//                log.info("【联系人】联系人延迟队列收到消息:{}", Base64.getEncoder().encodeToString(message.getBody()));
//            }
//            MsgPackFormatUtil.MqMessage mqMessage = MsgPackFormatUtil.mqDecode(message.getBody());
//            if (mqMessage == null) {
//                log.error("【联系人】联系人延迟队列消息解析失败:" + Base64.getEncoder().encodeToString(message.getBody()));
//                EMAIL_LOG.error("【联系人】联系人延迟队列消息解析失败,{}", Base64.getEncoder().encodeToString(message.getBody()));
//                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
//                return;
//            }
//            if (log.isInfoEnabled()) {
//                log.info("【联系人】开始消费联系人延迟队列消息:{}", JSON.toJSONString(mqMessage));
//            }
//            if (MemberTopics.MEMBER_REGROUP_MEMBER_TOPIC.equals(mqMessage.getTopic())) {
//                MemberCreationUpdMessage memberCreationUpdMessage = MsgPackFormatUtil.mapToObj(mqMessage.getData(),
//                        MemberCreationUpdMessage.class);
//                ServiceResult<Void> result = groupService.groupMember(memberCreationUpdMessage);
//                if (result.isSuccess()) {
//                    if (log.isInfoEnabled()) {
//                        log.info("【联系人】regroup member success, message:{}", memberCreationUpdMessage);
//                    }
//                } else {
//                    log.error("regroup member failed, reason: {}, message: {}", result,
//                            memberCreationUpdMessage);
//                }
//            } else if (MemberTopics.MEMBER_REGROUP_MEMBERS_TOPIC.equals(mqMessage.getTopic())) {
//                GroupingMessage groupingMessage = MsgPackFormatUtil.mapToObj(mqMessage.getData(),
//                        GroupingMessage.class);
//                ServiceResult<Void> result = groupService.groupMembers(groupingMessage);
//                if (result.isSuccess()) {
//                    if (log.isInfoEnabled()) {
//                        log.info("【联系人】regroup members success, message: {}", groupingMessage);
//                    }
//                } else {
//                    log.error("【联系人】regroup members failed,reason: {}, message: {}", result, groupingMessage);
//                }
//            } else if (MemberTopics.MEMBER_WECHAT_MEMBER_SYNC_DELAY_TOPIC.equals(mqMessage.getTopic())) {
//                WechatMemberSyncMessage wechatMemberSyncMessage = MsgPackFormatUtil.mapToObj(mqMessage.getData(),
//                        WechatMemberSyncMessage.class);
//                wechatService.sendWxSyncMessage(wechatMemberSyncMessage);
//            } else {
//                log.error("【联系人】消费联系人延迟队列消息失败:" + Base64.getEncoder().encodeToString(message.getBody()));
//                if (EMAIL_LOG.isErrorEnabled()) {
//                    EMAIL_LOG.error("【联系人】消费联系人延迟队列消息失败,{}", Base64.getEncoder().encodeToString(message.getBody()));
//                }
//            }
//            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//        } catch (Exception e) {
//            log.error("【联系人】处理联系人延迟队列消息异常:" + Base64.getEncoder().encodeToString(message.getBody()), e);
//            EMAIL_LOG.error("【联系人】处理联系人延迟队列消息异常{},{}", e.getMessage(), Base64.getEncoder().encodeToString(message.getBody()));
//            try {
//                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
//            } catch (IOException ex) {
//                log.error("【联系人】nack联系人延迟队列消息异常", ex);
//            }
//        }
//    }
//
//
//}
